#include <linSlaveTask.h>
#include <ColorMixingTask.h>
#include <applicationTask.h>
#include <SoftTimerTask.h>
#define rgbTable_Flow_1_(x)       rgbTable_Flow_1_##x
#define rgbTable_Flow_2_(x)       rgbTable_Flow_2_##x

TaskState_t        linsTaskState = TASK_STATE_INIT;
LIN_Device_Frame_t activelinsFrame;


//LIN通信帧
LIN_Device_Frame_t lins_cmd_table[] = {
    [0] = {
        .frame_id = 0x01,       //ModeSelection信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [1] = {
        .frame_id = 0x03,       //WorkModeSelection信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [2] = {
        .frame_id = 0x0a,       //SleepOrWake信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [3] = {
        .frame_id = 0x06,       //Indie3信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [4] = {
        .frame_id = 0x3C,
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [5] = {
        .frame_id = 0x3D,
        .msg_type = LIN_MSG_TYPE_TX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
};

void LIN_Slave_ISR(LIN_Device_Frame_t *frame)
{
    activelinsFrame = *frame;
    TM_PostTask(TASK_ID_LINS);  //中断接受到LIN帧就处理     LIN1   右靠灰线边
}

void hanldeLinMasterCommands(LIN_Device_Frame_t *frame)
{
    Color_CMD_Frame_t *data = (Color_CMD_Frame_t *)frame->data;        //一个8byte数组指针赋值给data指针，data是一个共用体
    switch(frame->frame_id)
    {
        case 0x3C:
          lins_cmd_table[5].responseValid = true;
          for (uint8_t i=0;i<8;i++)
              lins_cmd_table[5].data[i] = 0xAA+i;
        break;
        
        case 0x01:      //帧ID
          switch(data->command)
          {
              case 0x00:      //单色模式
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x10;
                tableIndex = data->Frame.tableIndex;
                rgbTablePt = rgbTable_SingleColor;
                TableMaxSize = TABLE_SIZE_SinClr;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval;
                SoftTimer_Start(&App1Timer);
              break;
              
              case 0x01:      //单色呼吸
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x20;
                tableIndex = data->Frame.tableIndex; 
                rgbTablePt = rgbTable_Breath;
                TableMaxSize = TABLE_SIZE_Brth;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval * 0.7;
                SoftTimer_Start(&App1Timer);
              break;
              
              case 0x02:      //多色呼吸
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x30;
                tableIndex = (data->Frame.tableIndex == 0x00 ? 0:(tableIndex >= TABLE_SIZE_Brth-1 ? 0:tableIndex + 1));
                rgbTablePt = rgbTable_Breath;
                TableMaxSize = TABLE_SIZE_Brth;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval * 0.7;
                SoftTimer_Start(&App1Timer);
              break;
              
              case 0x03:      //无极
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x40;
                tableIndex = (data->Frame.tableIndex == 0x00 ? 0:(tableIndex >= TABLE_SIZE_TransClr-1 ? 0:tableIndex + 1));;
                rgbTablePt = rgbTable_TransClr;
                TableMaxSize = TABLE_SIZE_TransClr;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval * 0.7;
                SoftTimer_Start(&App1Timer);
              break;
              
              case 0x04:      //警报
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x50;
                tableIndex = 0;
                rgbTablePt = rgbTable_Alarm;
                TableMaxSize = TABLE_SIZE_Alarm;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval * 0.3;
                SoftTimer_Start(&App1Timer);
              break;
              
              case 0x05:      //黄灯
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x60;
                tableIndex = 0;
                rgbTablePt = rgbTable_Yellow;
                TableMaxSize = TABLE_SIZE_Yellow;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval;
                SoftTimer_Start(&App1Timer);
              break;
          }
        break;
        
        case 0x06:      //帧ID
          switch(data->command)
          {
              case 0x06:      //多彩1
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x70;
                tableIndex = data->Frame.OnOff;
                rgbTablePt = rgbTable_Flow_1_1;
                TableMaxSize = TABLE_SIZE_Flow_1;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval* 0.7;
                SoftTimer_Start(&App1Timer);
              break;                    
              
              case 0x07:      //多彩2
                SoftTimer_Stop(&App1Timer);
                ModeSelection = 0x80;
                tableIndex = data->Frame.OnOff;
                rgbTablePt = rgbTable_Flow_2_1;
                TableMaxSize = TABLE_SIZE_Flow_2;
                App1Timer.interval = data->Frame.sectionTime;   //16位拼装，低在前高在后
                FadingTime = App1Timer.interval* 0.7;
                SoftTimer_Start(&App1Timer);
              break;
          }
        break;
        
        case 0x03:
        break;
        
        case 0x0A:
        break;
    }
}

void LINS_TaskHandler(void)
{
    switch(linsTaskState){
    case TASK_STATE_INIT:       //初始化部分只执行一次
      LINS_Init();      //LINSlave初始化
      LINS_RegisterLINS_IRQ(LIN_Slave_ISR);     //注册中断处理函数
      LINS_RegisterServices(lins_cmd_table, sizeof(lins_cmd_table)/sizeof(LIN_Device_Frame_t));         //注册lin帧
      linsTaskState = TASK_STATE_ACTIVE;
      break;
    case TASK_STATE_ACTIVE:
      hanldeLinMasterCommands(&activelinsFrame);
      break;   
    }
}